'use strict'

const { Controller } = require('egg')
class LoginConctroller extends Controller {
  /* 登录 */
  async h5Login() {
    const {
      ctx,
      app
    } = this
    try {
      ctx.validate({
        username: 'string',
        password: 'string'
      })
    } catch (error) {
      ctx.throw(400, `请求参数异常`)
    }

    const req = ctx.request.body
    const result = await ctx.service.user.h5Login(req)
    ctx.helper.success(ctx, result)
  }

  /* 注冊 */
  async register() {
    const {
      ctx,
      app
    } = this
    const req = ctx.request.body

    try {
      ctx.validate({
        username: 'string',
        password: 'string'
      })
    } catch (error) {
      ctx.throw(400, `请求参数异常`)
    }

    const result = await ctx.service.user.register(req)
    ctx.helper.success(ctx, { data: result })
  }

  // 管理后台登录
  async adminLogin() {
    const {
      ctx
    } = this
    const req = ctx.request.body
    try {
      ctx.validate({
        username: 'string',
        password: 'string'
      })
    } catch (error) {
      ctx.throw(400, `请求参数异常`)
    }

    const result = await ctx.service.user.adminLogin(req)
    ctx.helper.success(ctx, { data: result })
  }

  /**
   * admin退出登录
   */
  async adminLogout () {
    const { ctx } = this
    this.ctx.helper.success(ctx)
  }
}

module.exports = LoginConctroller